package se.mbaeumer.fxtodo.core.handler;

import java.util.Date;
import java.util.List;

import se.mbaeumer.fxtodo.core.model.Context;
import se.mbaeumer.fxtodo.core.model.EnumPriority;
import se.mbaeumer.fxtodo.core.model.EnumTaskStatus;
import se.mbaeumer.fxtodo.core.model.Task;
import se.mbaeumer.fxtodo.core.util.constants.FilterConstants;
import se.mbaeumer.fxtodo.db.DatabaseHandler;

public class TaskHandler {

	private static final TaskHandler INSTANCE = new TaskHandler();
	private TaskHandler(){};
	public static TaskHandler getInstance(){return INSTANCE;}
	
	private List<Task> tasks;
	
	public List<Task> loadTasksFromDB(DatabaseHandler dbh){
		this.tasks = dbh.getTasks();
		return this.tasks;
	}
	
	public int importTaskFromICS(DatabaseHandler dbh, Task task, int priorityId){
		return dbh.importTaskFromICS(task, priorityId);
	}

	public void importTaskFromXML(DatabaseHandler dbh, Task task){
		dbh.importTaskFromXML(task);
	}
	
	public Task createTask() {
		return null;
	}

	public Task updateTask(DatabaseHandler dbh, Task task, int contextId, int statusId, int priorityId) {
		if (task.getId() == -1){
			dbh.createTask(task, contextId, statusId, priorityId);
		}else{
			dbh.updateTask(task, contextId, statusId, priorityId);
		}
		return null;
	}
	
	public void moveTask(DatabaseHandler dbh, Task task, int contextId){
		dbh.moveTask(task, contextId);
	}

	public void deleteTask(DatabaseHandler dbh, int taskId) {
		dbh.deleteTask(taskId);
	}
	
	public void deleteTasksFromContext(DatabaseHandler dbh, int contextId){
		dbh.deleteTasksFromContext(contextId);
	}

	public List<Task> getTasks(DatabaseHandler dbh) {
		return this.loadTasksFromDB(dbh);
	}

	/**
	 * Gets all tasks with a given context
	 * @param dbh the database handler
	 * @param contextId the id of the context
	 * @return a list of tasks
	 */
	public List<Task> getTasksByContext(DatabaseHandler dbh, int contextId) {
		return dbh.getTasksByContext(contextId);
	}

	public List<Task> getTasksByType(int taskTypeId) {
		return null;
	}

	public List<Task> getTasksByStatus(DatabaseHandler dbh, int statusId, int contextId) {
		return dbh.getTasksByStatus(statusId, contextId);
	}

	public Context getTask(int id) {
		return null;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}
	
	public List<Task> getTasks(DatabaseHandler dbh, int contextId, String period, int statusId, int priorityId){
		return dbh.getTasks(contextId, period, statusId, priorityId);
	}
	
	public Task createDummyTask(){
		//TODO: Use constants instead of hard-coded strings
		Task task = new Task.TaskBuilder().
				id(-1).
				title("<New>").
				description("<New>").
				created(new Date()).
				dueDate(new Date()).
				status(EnumTaskStatus.Open).
				priority(EnumPriority.Normal).
				build();
		return task;
	}
	
	public List<Task> getTasksByTimePeriod(DatabaseHandler dbh, String period, int contextId){
		if (period.equals(FilterConstants.FILTER_NONE)){
			return dbh.getTasksByContext(contextId); 
		}
		
		return dbh.getTasksByTimePeriod(period, contextId);
	}
	

}
